package com.edu.zql;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;

/**
 * Created by User on 2017/9/25.
 */
public class asdasdasdasdsad {

    public static void main(String[] args) throws FileNotFoundException {
        List list = new ArrayList();
        Map map = new HashMap();
        list.add(newMap("name", "AP/网业")); // 0
        list.add(newMap("name", "BOBARRY/柏佰利")); // 1
        list.add(newMap("name", "BUMBLEBEE/大黄蜂")); // 2
        list.add(newMap("name", "Bandridge/百接吉")); // 3
        list.add(newMap("name", "Basilisk/毒蜥")); // 4
        list.add(newMap("name", "CARVE/凯旺")); // 5
        list.add(newMap("name", "GY OCC")); // 6
        list.add(newMap("name", "AP/网业(括号)")); // 7
        list.add(newMap("name", "AP/网业(括号括号)")); // 8
        list.add(newMap("name", "BUMBLEBEE")); // 9
        list.add(newMap("name", "BUMBLEBEE(括号)1")); // 10
        list.add(newMap("name", "BUMBLEBEE(括号括号)")); // 11
        list.add(newMap("name", "CANARE")); //12
        list.add(newMap("name", "CARVE/凯旺(括号)1")); //13

        Integer i = null;
        i = new FindInList2(list, "name", "CANARE").find();
        System.out.println(i); // 12
        i = new FindInList2(list, "name", "GY OCC").find();
        System.out.println(i); // 6
        i = new FindInList2(list, "name", "CARVE/凯旺(abc)1").find();
        System.out.println(i); // 13
        i = new FindInList2(list, "name", "AP/网业(括号括号)").find();
        System.out.println(i); // 8
        i = new FindInList2(list, "name", "BUMBLEBEE").find();
        System.out.println(i); // 9
        i = new FindInList2(list, "name", "BUMBLEBEE(abc)1").find();
        System.out.println(i); // 10
        i = new FindInList2(list, "name", "BUMBLEBEE(括号括号)").find();
        System.out.println(i); // 11


    }

    static Map newMap(String key, String value) {
        Map map = new HashMap();
        map.put(key, value);
        return map;
    }

    static class FindInList2 {
        List list;
        String key;
        String targetValue;
        Integer loopStartIndex;
        Integer matchEqualCheck;
        EnumSet<EqualCheck> equalCheckSet;


        public FindInList2(List list, String key, String targetValue) {
            this.list = list;
            this.key = key;
            this.targetValue = targetValue;
            this.loopStartIndex = 0;
            equalCheckSet = EnumSet.of(EqualCheck.equal, EqualCheck.replace, EqualCheck.contain);
            this.matchEqualCheck = equalCheckSet.size();
        }

        enum EqualCheck{
            equal {
                @Override
                public Boolean equal(String var1, String var2) {
                    return var1.equals(var2);
                }
            },
            replace {
                @Override
                public Boolean equal(String var1, String var2) {
                    return var1.replaceAll("\\(.*?\\)", "").equals(var2.replaceAll("\\(.*?\\)", ""));
                }
            },
            contain {
                @Override
                public Boolean equal(String var1, String var2) {
                    return var1.contains(var2) || var2.contains(var1);
                }
            };

            public abstract Boolean equal(String var1, String var2);
        }

        public Integer find() {
            for (int i = loopStartIndex; i < list.size(); i++) {
                Map map = (Map) list.get(i);
                String value = (String) map.get(key);
                Iterator<EqualCheck> iterator = equalCheckSet.iterator();
                for (int j = 0; iterator.hasNext() && j < matchEqualCheck; j++) {
                    EqualCheck equalCheck = iterator.next();
                    if (equalCheck.equal(value, targetValue)) {
                        if (j == 0) return i;
                        matchEqualCheck = j;
                        loopStartIndex = i + 1;
                        Integer integer = find();
                        if (integer != -1) return integer;

                        return i;
                    }
                }
            }

            return -1;

        }

    }



    static class FindInList {
        List list;
        String key;
        String targetValue;
        Integer matchIndex;
        Integer loopStartIndex;
        Integer matchEqualCheck;
        EnumSet<EqualCheck> equalCheckSet = EnumSet.allOf(EqualCheck.class);


        public FindInList(List list, String key, String targetValue) {
            this.list = list;
            this.key = key;
            this.targetValue = targetValue;
            this.matchIndex = 0;
            this.loopStartIndex = 0;
            this.matchEqualCheck = equalCheckSet.size();
        }

        public Integer find12323() {
            for (int i = 0; i < list.size(); i++) {
                Map map = (Map) list.get(i);
                String value = (String) map.get(key);
                if (value.equals(targetValue)) {

                    return i;
                } else if (firstCheck(value, targetValue)) {
                    matchIndex = i;
                    Integer equal = findEqual();
                    if (equal != -1) return equal;

                    return matchIndex;
                } else if (secondCheck(value, targetValue)) {
                    matchIndex = i;
                    Integer findEqualFirstCheck = findEqualFirstCheck();
                    if (findEqualFirstCheck != -1) return findEqualFirstCheck;

                    return matchIndex;
                }
            }


            return -1;
        }

        public Boolean firstCheck(String value, String targetValue) {
            return value.replaceAll("\\(.*?\\)", "").equals(targetValue.replaceAll("\\(.*?\\)", ""));
        }

        public Boolean secondCheck(String value, String targetValue) {
            return value.contains(targetValue) || targetValue.contains(value);
        }

        public Integer findEqual() {
            for (int i = matchIndex + 1; i < list.size(); i++) {
                Map map = (Map) list.get(i);
                String value = (String) map.get(key);
                if (value.equals(targetValue)) return i;
            }

            return -1;
        }

        public Integer findEqualFirstCheck() {
            for (int i = matchIndex + 1; i < list.size(); i++) {
                Map map = (Map) list.get(i);
                String value = (String) map.get(key);
                if (value.equals(targetValue)) {

                    return i;
                } else if (firstCheck(value, targetValue)) {
                    matchIndex = i;
                    Integer equal = findEqual();
                    if (equal != -1) return equal;

                    return matchIndex;
                }
            }

            return -1;



        }


        public Integer find() {
            for (int i = loopStartIndex; i < list.size(); i++) {
                Map map = (Map) list.get(i);
                String value = (String) map.get(key);
                Iterator<EqualCheck> iterator = equalCheckSet.iterator();
                for (int j = 0; iterator.hasNext() && j < matchEqualCheck; j++) {
                    EqualCheck equalCheck = iterator.next();
                    if (equalCheck.equal(value, targetValue)) {
                        if (j == 0) return i;
                        matchEqualCheck = j;
                        loopStartIndex = i + 1;
                        Integer integer = find();
                        if (integer != -1) return integer;

                        return i;
                    }
                }
            }

            return -1;

        }

        enum EqualCheck{
            equal {
                @Override
                public Boolean equal(String var1, String var2) {
                    return var1.equals(var2);
                }
            },
            replace {
                @Override
                public Boolean equal(String var1, String var2) {
                    return var1.replaceAll("\\(.*?\\)", "").equals(var2.replaceAll("\\(.*?\\)", ""));
                }
            },
            contain {
                @Override
                public Boolean equal(String var1, String var2) {
                    return var1.contains(var2) || var2.contains(var1);
                }
            };

            public abstract Boolean equal(String var1, String var2);
        }
    }








    private static void asdasd() {
        String keyword = "衬衫男  长袖 asda";

        String[] split = keyword.split(" +");
        for (String s : split)
            System.out.println(s);


        Map map1 = new HashMap();
        map1.put("1", "1");
        map1.put("2", "2");
        map1.put("3213", "2133");
        map1.put("4", "4");
        map1.put("213", "1231");
        Map map2 = new HashMap();
        map2.put("1", "1");
        map2.put("2", "2");
        map2.put("4", "4");
        map2.put("213", "1231");


        MapDifference difference = Maps.difference(map1, map2);

        Map map = difference.entriesDiffering();
        Map map3 = difference.entriesInCommon();
        Map map4 = difference.entriesOnlyOnLeft();
        Map map5 = difference.entriesOnlyOnRight();
        System.out.println(map3);
    }

}
